import EventEmitter from 'events'
import EextModel from './text_model'

interface SelectionType {
  anchor: number,
  focus: number
}

export class SelectionModel extends EventEmitter {
  textModel_: EextModel
  selection_: SelectionType
  constructor(textModel: EextModel) {
    super()
    this.textModel_ = textModel;
    this.selection_ = { anchor: 0, focus: 0 }
  }

  setSelection(selection: SelectionType) {
    if (selection
      && this.selection_
      && selection.anchor === this.selection_.anchor
      && selection.focus === this.selection_.focus) {
      return
    }
    this.selection_ = selection ? { ...selection } : this.selection_
  }
  getSelection() {
    return this.selection_
  }
  isCollapsed() {
    return this.selection_ && this.selection_.anchor === this.selection_.focus
  }
  isBackkward() {
    return this.selection_ && this.selection_.anchor > this.selection_.focus
  }
}

export default SelectionModel